#-*-coding:utf-8-*-

import os, sys
import pycurl
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

def getKebiaoHtml(opts):
    grade = opts["grade"]
    start = opts["start"]
    end   = opts["end"]
    term  = opts["term"]

    uid_prefix = "U%d" % grade
    url_prefix = "http://bksjw.hust.edu.cn/reportJsp/showReport.jsp?raq=studentKebiao.raq&xqh=%d&sfid=" % term
    result_dir = "kebiao%d_%d/" % (term, grade)

    if not os.path.exists(result_dir):
        os.makedirs(result_dir)

    c = pycurl.Curl()
    c.setopt(pycurl.REFERER, "http://bksjw.hust.edu.cn/aam/score/report/query_personal_score.jsp?cdbh=225")
    c.setopt(pycurl.TIMEOUT, 4)

    i = start
    while i < end:
        uid_suffix = str(i)
        uid = uid_prefix + "0"*(4-len(uid_suffix)) + uid_suffix
        url = url_prefix + uid
        print "downloading page for", uid

        c.setopt(pycurl.URL, url)
        b = StringIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        try:
            c.perform()
        except Exception as error:
            print "Error", error
            continue

        data = b.getvalue().decode("gbk").encode("utf-8")
    
        fp = open(result_dir + uid + ".html", "w")
        fp.write(data)
        fp.close()

        i += 1

if __name__ == "__main__":
    getKebiaoHtml({
        "grade" : 2008, 
        "start" : 14075, 
        "end"   : 14080,
        "term"  : 20111
    })
